var path=$("#pathId").val();

function autoinput(){
    var url =path+"/aem/client/loadDepartMentCodeInfo";
    $.post(url, {
        "cid":cid
    }, function(data) {
        if (data.status == 200) {
            insertOptions(data.data);
        }else{

        }
    },"json");
}

var insertOptions = function(data) {
    var result = new Array();
    $.each(data, function(i, item){
        result.push(item);
    });
    $("#departmentId").autocomplete({
        mustMatch:true,
        source: result,
        select: function(event, ui) {
            var itemVa=ui.item.value;
            var arr=itemVa.split("-");
            var name = itemVa.substr(0, itemVa.lastIndexOf('-'))
            var code = arr[arr.length-1];
            ui.item.value=name;
            $("#deptCode").val(code);
            staffPropertyInfo.click = 1;
        }
    })
}


$('#edit_staff').click(function(){
    var flag=true;
    var idCardReg =  /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    var name = $('#name').val();
    var idCard = $("#idCard").val();
    var email = $("#email").val();
    var mobile = $('#phone').val();
    var cid = $('#cid').val();
    var staffId = $('#staffId').val();
    var departmentId = $('#departmentId').val();//部门名称
    var deptCode = $('#deptCode').val();//部门邀请码
    var deptId = $('#deptId').val();//部门id
    var birthday = $('#birthday').val();
    var birth;
    var email = $('#email').val();
    var detailAddress = $('#detailAddress').val();
    var address = $('#address').val();
    var token=$("#accessToken").val();
    var oldOrgName=$("#oldOrgName").val();
    var oldOrgId=$("#oldOrgId").val();
    /*if (staffPropertyInfo.click==0){
        func_artdialog("提示","部门邀请码无效");
        flag=false;
        staffPropertyInfo.click = 0;
    }*/
    if(!name){
        func_artdialog("提示","姓名不能为空");
        $('#name').focus();
        flag=false;
    }
    if(!departmentId){
        func_artdialog("提示","部门邀请码不能为空");
        $('#departmentId').focus();
        flag=false;
    }
    if(deptCode){
        deptId = ""
    }

    if(!deptCode){
        if((departmentId != oldOrgName) || (deptId != oldOrgId)){
            func_artdialog("提示","部门邀请码无效！");
            flag=false;
        }
    }
    if($('#idCard').data('required') == 1){
        if(!idCard){
            func_artdialog("提示","身份证号不能为空");
            $('#idCard').focus();
            flag=false;
        }
    }
    if($('#birthday').data('required') == 1){
        if(!birthday){
            func_artdialog("提示","出生日期不能为空");
            $('#birthday').focus();
            flag=false;
        }
    }

    if($('#email').data('required') == 1){
        if(!email){
            func_artdialog("提示","邮箱不能为空");
            $('#email').focus();
            flag=false;
        }
    }
    if($('#address').data('required') == 1){
        if(!address){
            func_artdialog("提示","地址不能为空");
            $('#address').focus();
            flag=false;
        }
    }
    if($('#detailAddress').data('required') == 1){
        if(!detailAddress){
            func_artdialog("提示","地址不能为空");
            $('#detailAddress').focus();
            flag=false;
        }
    }
    if($('#male').data('required') == 1 && $('#female').data('required') == 1){
        var val=$('input:radio[name="sex"]:checked').val();
        if(val==null){
            func_artdialog("提示","性别不能为空");
            flag=false;
        }
    }
    if(idCard){
        if(idCardReg.test(idCard) === false) {
            func_artdialog("提示","身份证格式不正确");
            $('#idCard').focus();
            flag=false;
        }
    }
    if(email){
        if(validationEmail(email) == false){
            func_artdialog("提示","邮箱格式不正确");
            $('#email').focus();
            flag=false;
        }
    }
    if(address){
        var arr = address.split(' ');
        $('#provinceId').val(arr[0]);
        $('#cityId').val(arr[1]);
        $('#areaId').val(arr[2]);
        var provinceId = $('#provinceId').val();
        var cityId = $('#cityId').val();
        var areaId = $('#areaId').val();
    }
    if(birthday){
        birth = new Date(birthday);
    }

    var sex=$('input:radio[name="sex"]:checked').val();

   /* if(!deptCode) {
        if (flag) {
            $.ajax({
                url: path + "/aem/staffPropertyInfo/checkDeptExist",
                type: "post",
                async: false,
                data: {
                    "cid": cid,
                    "code": departmentId,
                },
                dataType: "json",
                error: function () {},
                success: function (data) {
                    if (data.status == "201") {
                        func_artdialog("提示", "该部门不存在!");
                        flag = false;

                    }
                }
            });
        }
    }*/

        if(flag){
            $.ajax({
                url: path +"/aem/staffPropertyInfo/updateStaffPropertyInfo",
                type: "post",
                async: true,
                data:{
                    "cid":cid,
                    "staffId":staffId,
                    "userName":name,
                    "mobile":mobile,
                    "idCard":idCard,
                    "birthday":birth,
                    "organization":departmentId,
                    "deptCode":deptCode,
                    "departmentId":deptId,
                    "email":email,
                    "sex":sex,
                    "provinceId":provinceId,
                    "cityId":cityId,
                    "areaId":areaId,
                    "detailAddress":detailAddress,
                    "token":token
                },
                dataType: "json",
                error: function(){},
                success: function(data){
                    if(data.status == "200"){
                        func_successlog("提示","个人资料修改成功!");
                        /* setTimeout(function () {
                             history.back();
                         }, 500)*/
                    }else{
                        func_artdialog("提示","个人资料修改失败!");
                    }
                }
            });
        }

})

function func_successlog(name,value) {
    var d = dialog({
        title: name,
        fixed: true,
        width: 200,
        height: 100,
        content: "<center style='font-size:15px; line-height:100px;'>" + value + "</center>",
        okValue: "确定",
        ok: function () {
            history.back();
        }
    });
    d.show();
}
function func_artdialog(name,value){
    var d = dialog({
        title: name,
        fixed: true,
        width: 200,
        height:100,
        content:"<center style='font-size:15px; line-height:100px;'>"+value+"</center>",
        okValue: "确定",
        ok: function () {
            return true;
        }
    });
    d.show();
}

function StringEmpty(content) {
    if (content == undefined || content == "" || content == null || content=="undefined" || content =="null") {
        return false;
    } else {
        return true;
    }
}
